//AUTHOR:  Aaron Kaufman
 //Latest Edit:  12/2/2012
/*
 * Governs the assignment of subprogram chairs by the Program Chair.
 */

package gui;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

import model.ProgramChair;
import model.Reviewer;

public class SPCAssigner extends JPanel
{
    private ProgramChair my_chair;
    private ProgramChairViewPanel my_parent_panel;
    private JPanel my_grid;
    private JPanel my_card_panel;
    
    public SPCAssigner(ProgramChair the_program_chair, ProgramChairViewPanel the_parent, JPanel the_card_panel)
    {
        my_card_panel = the_card_panel;
        my_parent_panel = the_parent;
        my_chair = the_program_chair;
        
        GridLayout g = new GridLayout (0,2);

        my_grid = new JPanel(g);
        JPanel buffer = new JPanel();
        
        this.add(buffer);
        this.add(my_grid);
        this.add(buffer);
        
        g.setVgap(5);

        rebuildLayout(my_grid);
    }
    
    private void rebuildLayout(JPanel the_c)
    {
        the_c.removeAll();
        the_c.add(getBorderedLabel("Designate SPCs:"));
        the_c.add(new JLabel());
        
        for (String rev : my_chair.getReviewers().keySet())
        {
            the_c.add(new JLabel(rev));
            if (my_chair.getSubprogramChairs().get(rev) == null)
            {
                the_c.add(createAssignSPCButton(rev));
            }
            else
            {
                the_c.add(new JLabel("Already assigned."));
            }
        }
        the_c.add(createBackToPapersButton(my_card_panel));
    }
    
    private Component createBackToPapersButton(final JPanel the_card_panel)
    {
        JButton button = new JButton("Back to Conference Papers");
        button.addActionListener(new AbstractAction(){

            @Override
            public void actionPerformed(ActionEvent the_e)
            {
                CardLayout card = (CardLayout)the_card_panel.getLayout();
                card.show(the_card_panel, ProgramChairViewPanel.MANUSCRIPT_PANEL);
            }
        });
        return button;
    }

    private JButton createAssignSPCButton(final String the_user)
    {
        final JButton button = new JButton("Assign As Subprogram Chair");
        button.addActionListener(new AbstractAction(){

            @Override
            public void actionPerformed(ActionEvent the_arg0)
            {
                my_chair.designateSubprogramChair(the_user);
                button.setVisible(false);
                my_parent_panel.rebuild();
                
                
            }
        });
        return button;
    }


    private JLabel getBorderedLabel(String the_string)
    {
        
        JLabel label = new JLabel(the_string);
        label.setBorder(new LineBorder(Color.BLACK));
        return label;
        
    }
    

}
